Skip to content

Fix error handling in needRules() - #1207

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
JakobDev:errorhandle
Jan 27, 2026
Merged

Fix error handling in needRules()#1207
alecthomas merged 1 commit into
alecthomas:masterfrom
JakobDev:errorhandle

Conversation

@JakobDev

Copy link
Copy Markdown
Contributor

I discovered a little bug while trying to import a Pygments lexer: needRules() calls fetchRules() inside compileOnce. This means that fetchRules() is only called the first time needRules() is called. So if the needRules() is called by Tokenise() the error will only be handled the first time. Any call to Tokenise() after that will cause a panic, as no rules are loaded. To fix that, we need to store the error in the struct.

If you want to test it, you can try this invalid lexer in chromad:

<lexer>
  <config>
    <name>Test</name>
  </config>
  <rules>
  </rules>
</lexer>

Just select the Lexer and start typing.

@alecthomas

Copy link
Copy Markdown
Owner

Nice catch, thanks. I'm actually pretty surprised the linter didn't pick up that err not being used...

@alecthomas
alecthomas merged commit fef8258 into alecthomas:master Jan 27, 2026
2 checks passed
@JakobDev

JakobDev commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

I'm actually pretty surprised the linter didn't pick up that err not being used...

The problem is, that err is only set the first time the function is called. That's hard for an linter to catch, as it's a logic error.

@JakobDev
JakobDev deleted the errorhandle branch January 27, 2026 21:14
@alecthomas

Copy link
Copy Markdown
Owner

errcheck is designed to pick up exactly this situation: error values written to but not read from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants